<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ include file="/WEB-INF/page/public/taglib.jsp"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 
 <title>友情链接</title>
<%@ include file="/WEB-INF/page/public/contentmeta.jsp"%>
<link rel="stylesheet" href="<%=basePath%>css/global.css" media="all" />
<link rel="stylesheet" href="<%=basePath%>plugins/font-awesome/css/font-awesome.min.css">
<link rel="stylesheet" href="<%=basePath%>css/system.css" media="all">
<link rel="stylesheet" href="<%=basePath%>css/tab.css" media="all" />
	
<style type="text/css">
	.addalldiv,.addfilediv,.addurldiv {display: none;}
	.addalldiv .selecttype {margin-top: 20px;margin-left: 10px;}
	.addfilediv .addfile {margin-top: 20px;margin-left: 10px;}
	.addurldiv .addurl {margin-top: 20px;margin-left: 10px;}
</style>

</head>
<body>
<div class="admin-tab admin-user">

	<div class="tab-condition">
		<form class="layui-form clefix" id="htform" action="/linkurl" method="post">
			<div class="layui-form-item">
				<div class="layui-input-block">
					<input type="text" name="keyWord" id="keyWord" autocomplete="off" placeholder="请输入关键字" class="layui-input" value="${pager.params.keyWord}">
				</div>
			</div>
			<div class="layui-form-item">
				<div class="layui-input-block" style="width: 170px;">
					<select name="provinceid" id="provinceid" lay-filter="provinceid" class="typeclass">
						<option value="">请选择省份</option>
					</select>
				</div>
			</div>
			<div class="layui-form-item">
				<div class="layui-input-block" style="width: 170px;">
					<select name="type" id="type" lay-filter="type" class="typeclass">
						<option value="">请选择类别</option>
						<option value="-1" <c:if test="${pager.params.type == -1}">selected</c:if>>全部</option>
						<%-- <option value="0" <c:if test="${pager.params.type == 0}">selected</c:if>>文章</option> --%>
						<option value="1" <c:if test="${pager.params.type == 1}">selected</c:if>>文件</option>
						<option value="2" <c:if test="${pager.params.type == 2}">selected</c:if>>链接</option>
					</select>
				</div>
			</div>
			<div class="layui-form-item">
				<input name="skipToPage" id="skipToPage" type="hidden" value="${pager.pageNo}"> 
				<input type="hidden" id="pageNo" name="pageNo" value="${pager.pageNo}"> 
				<input type="hidden" id="totalPage" name="totalPage" value="${pager.totalPages}">
				<input type="hidden" id="proid" name="proid" value="${pager.params.proid}">
				<button class="layui-btn TM-btn-search searchbtn" type="button">
					<i class="layui-icon layui-unselect layui-tab-close">&#xe615;</i>搜索
				</button>
			</div>
		</form>
		<div class="TM-tab-info">
			<a class="layui-btn layui-btn-primary layui-btn-small addbtn"><i class="layui-icon">&#xe654;</i>添加</a>
			<a class="layui-btn layui-btn-primary layui-btn-small pldelete"><i class="layui-icon">&#xe640;</i>删除</a>
			<a class="layui-btn layui-btn-primary layui-btn-small refreshbtn"><i class="layui-icon">&#x1002;</i>刷新</a>
		</div>
	</div>
	<div class="layui-field-box">
		<table class="site-table table-hover TM-table3">
			<thead>
				<tr>
					<th style="width: 5%;"><input type="checkbox" id="selected-all"/></th>
					<th style="width: 55%;">标题</th>
					<th style="width: 10%;">类别</th>
					<th style="width: 10%;">省份</th>
					<th style="width: 20%;">操作</th>
				</tr>
			</thead>
			<tbody>
				<c:forEach var="obj" items="${pager.resultList}" varStatus="o1">
					<tr>
						<td><input type="checkbox" name="select-all-cbox" value="${obj.id}"/></td>
						<td>
							<c:if test="${obj.type == 1}">
								<a href="${obj.content}" target="_blank">${obj.title}</a>
							</c:if>
							<c:if test="${obj.type == 2}">
								<a href="/file/${obj.content}">${obj.title}</a>
							</c:if>
						</td>
						<td>
							<c:if test="${obj.type == 0}">文章</c:if>
							<c:if test="${obj.type == 1}">链接</c:if>
							<c:if test="${obj.type == 2}">文件</c:if>
						</td>
						<td>${obj.provincename}</td>
						<td>
							<!-- 文章 -->
							<c:if test="${obj.type == 0}">
								<a class="layui-btn layui-btn-normal layui-btn-mini">查看</a>
								<a class="layui-btn layui-btn-normal layui-btn-mini">修改</a>
								<a class="layui-btn layui-btn-normal layui-btn-mini deleteart" idddata="${obj.id}">删除</a>
							</c:if>
							<!-- 链接 -->
							<c:if test="${obj.type == 1}">
								<a href="${obj.content}" target="_blank" class="layui-btn layui-btn-normal layui-btn-mini">打开</a>
								<a class="layui-btn layui-btn-normal layui-btn-mini upurl" iddata="${obj.id}" titledata="${obj.title}" urldata="${obj.content}" proiddata="${obj.provinceid}">修改</a>
								<a class="layui-btn layui-btn-normal layui-btn-mini deleteart" idddata="${obj.id}">删除</a>
							</c:if>
							<!-- 文件 -->
							<c:if test="${obj.type == 2}">
								<a href="/file/${obj.content}"  class="layui-btn layui-btn-normal layui-btn-mini">下载</a>
								<a class="layui-btn layui-btn-normal layui-btn-mini deleteart" idddata="${obj.id}">删除</a>
							</c:if>
						</td>
					</tr>
				</c:forEach>
			</tbody>
		</table>
		<div class="admin-table-page">
            <div id="page" class="page"></div>
        </div>
	</div>
</div>

<!-- 添加总弹窗 -->
<div class="addalldiv">
	<div class="selecttype">
		<div class="layui-form-item">
			<label class="layui-form-label" style="width: 150px;">请选择要添加的类型 :</label>
			<div class="layui-input-inline">
				<select name="types" id="types" lay-filter="types"class="layui-input">
					<!-- <option value="0">文章</option> -->
					<option value="1">文件</option>
					<option value="2">链接</option>
				</select>
			</div>
		</div>	
	</div>
</div>

<!-- 添加文件弹窗 -->
<div class="addfilediv">
	
	<div class="addfile">
		<form action="/savelinkfile"  method="post" id="fileform" enctype="multipart/form-data">
			<div class="layui-form-item">
				<label class="layui-form-label"><i>*</i><span>标题: </span></label>
				<div class="layui-input-block" style="width: 60%;">
					<input type="text" name="filetitle" id="filetitle" autocomplete="off" class="layui-input" placeholder="请输入标题">
				</div>
			</div>
			<div class="layui-form-item">
				<label class="layui-form-label"><i>*</i><span>省份: </span></label>
				<div class="layui-input-block" style="width: 60%;">
					<select name="fileproid" id="fileproid" lay-filter="fileproid" class="layui-input">
						<option value="">请选择省份</option>
					</select>
				</div>
			</div>
			<div class="layui-form-item">
				<label class="layui-form-label"><i>*</i><span>文件: </span></label>
				<div class="layui-input-block" style="width: 60%;">
					<input type="file" name="file" id="file" class="layui-input">
				</div>
			</div>
		</form>
	</div>	
</div>

<!-- 添加链接弹窗 -->
<div class="addurldiv">
	<div class="addurl">
		<form method="post" action="/savelinkurl" id="urlform">
			<div class="layui-form-item">
				<label class="layui-form-label"><i>*</i><span>标题: </span></label>
				<div class="layui-input-block" style="width: 60%;">
					<input type="text" name="urltitle" id="urltitle" autocomplete="off" class="layui-input" placeholder="请输入标题">
				</div>
			</div>
			<div class="layui-form-item">
				<label class="layui-form-label"><i>*</i><span>省份: </span></label>
				<div class="layui-input-block" style="width: 60%;">
					<select name="urlproid" id="urlproid" lay-filter="urlproid" class="layui-input">
						<option value="">请选择省份</option>
					</select>
				</div>
			</div>
			<div class="layui-form-item">
				<label class="layui-form-label"><i>*</i><span>链接: </span></label>
				<div class="layui-input-block" style="width: 60%;">
					<input type="text" name="url" id="url" autocomplete="off" class="layui-input" placeholder="请输入网址" value="http://">
				</div>
			</div>
			<input type="hidden" value="" name="urlartid" id="urlartid">
		</form>
	</div>
	
</div>



<script>
	
	layui.config({
		base : 'plugins/layui/modules/'
	});
	
	layui.use([ 'element', 'laypage',"laytpl", 'form', 'laydate', 'layer' ],function() {
		var $ = layui.jquery, 
			laypage = layui.laypage, 
			layer = layui.layer,
			laytpl = layui.laytpl,
			element = layui.element(); //Tab的切换功能，切换事件监听等，需要依赖element模块
		var form = layui.form();
		
		//加载省份下拉框
		var provicnid = $("#proid").val();
		var html = province.getalluserprovincelink(provicnid);
		$("#provinceid").html(html);
		//更新页面
    	form.render();
		
		var addallindex;
		//添加文章
		$(".addbtn").on("click",function(){
			addallindex = layer.open({
				type:1,
	   		  	btn:['确定','取消'],
	           	btnAlign:'c',
	           	shadeClose:true,
		  		title:"选择类型",
		  		zIndex: 999,
		  		content: $(".addalldiv"),
		  		area:['30%','25%'],
		  		maxWidth:800,
		  		yes: function(layero, index){
		  			var types = $("#types").val();
		  			layer.close(addallindex);
		  			//根据选择的type跳到不同的添加页面
		  			if (types == 1) {
		  				//文件
		  				var provicnid = -1
		  				var provincehtml = province.getallprovinceilst(provicnid);
		  				$("#fileproid").html(provincehtml);
		  				layer.open({
		  					type:1,
		  					btn:['确定','取消'],
		  					btnAlign:'c',
		  					shadeClose:true,
		  					title:"添加文件",
		  					content: $(".addfilediv"),
		  			  		area:['35%','40%'],
		  			  		yes:function(){
		  			  			var filetitle = $("#filetitle").val();
		  			  			var file = $("#file").val();
		  			  			if (filetitle == null || filetitle == '') {
		  			  				layer.tips("请输入标题", $("#filetitle"), {
		  		 						tips: [2,tipColor],
		  		 	     				tipsMore: true
		  		 	     			});
		  			  				return false;
		  			  			} else if (file == null || file == '') {
			  			  			layer.tips("请选择文件", $("#file"), {
		  		 						tips: [2,tipColor],
		  		 	     				tipsMore: true
		  		 	     			});
		  			  				return false;
		  			  			}
		  			  			$("#fileform").submit();
		  			  		}
		  				});
		  			} else if (types == 2) {
		  				//链接
		  				var provicnid = -1
		  				var provincehtml = province.getallprovinceilst(provicnid);
		  				$("#urlproid").html(provincehtml);
		  				form.render();
		  				layer.open({
		  					type:1,
		  					btn:['确定','取消'],
		  					btnAlign:'c',
		  					shadeClose:true,
		  					title:"添加网址",
		  					zIndex: 999,
		  					content: $(".addurldiv"),
		  			  		area:['35%','40%'],
		  			  		maxWidth:800,
		  			  		yes:function(layero, index){
		  			  			$("#urlform").submit();
		  			  		}
		  				});
		  			} else {
		  				//文章
		  				
		  			}
		  		}
			});
		});
		
		//删除文章
		$(".deleteart").on("click",function(){
			var id = $(this).attr("idddata");
			layer.confirm('确定删除吗？', {
				btn: ['确定','取消'] //按钮
			}, function(){
				$.ajax({
					type : "post",
					data : {id : id},
					url : "/deletelink",
					dataType : "json",
					success : function(data){
						layer.msg("删除成功",{icon:1,time:500},function(){
							window.location.reload();
						});					
					}
				});
			}, function(){
			});
		});
		
		//修改链接
		$(".upurl").on("click",function(){
			var id = $(this).attr("iddata");
			var url = $(this).attr("urldata");
			var title = $(this).attr("titledata");
			var proid = $(this).attr("proiddata");
			$("#urltitle").val(title);
			$("#urlproid").val(proid);
			$("#url").val(url);
			$("#urlartid").val(id);
			var provincehtml = province.getallprovinceilst(proid);
			$("#urlproid").html(provincehtml);
			form.render();
			//打开弹窗
			layer.open({
				type:1,
				btn:['确定','取消'],
				btnAlign:'c',
				shadeClose:true,
				title:"修改网址",
				zIndex: 999,
				content: $(".addurldiv"),
			  	area:['35%','40%'],
			  	maxWidth:800,
			  	yes:function(layero, index){
			  		$("#urlform").submit();
			  	}
			});
		});
		
		
		//点击搜索
		$(".searchbtn").on("click",function(){
			$("#skipToPage").val(1);
			$("#htform").submit();
		});
		
		//批量删除
		$(".pldelete").on("click",function(){
			
			var ids;
    		//利用.each获取
			$("input:checkbox[name='select-all-cbox']:checked").each(function(i){
				if (i == 0) {
					ids = $(this).val();
				} else {
					ids += ","+$(this).val();
				}
			});
			layer.confirm('确定删除吗？', {
				btn: ['确定','取消'] //按钮
			}, function(){
				$.ajax({
					type : "post",
					data : {ids : ids},
					url : "/pldeletelink",
					dataType : "json",
					success : function(data){
						layer.msg("删除成功",{icon:1,time:700},function(){
							window.location.reload();
						});					
					}
		    	})
			}, function(){
			});
		});
		
		
		//check-box选择全部
		$("#selected-all").on("click",function(){
			if ($("#selected-all").is(":checked")){
				$("[name='select-all-cbox']").prop("checked",true);
			} else {
				$("[name='select-all-cbox']").prop("checked",false);
			}
		});
		
		var pageNo = $("#pageNo").val();
    	var totalPage = $("#totalPage").val();
        //page
        laypage({
            cont: 'page',
            curr: pageNo//'${pager.pageNo}'
            ,pages: totalPage//'${pager.totalPages}' //总页数
            ,
            groups: 5 //连续显示分页数
            ,
            jump: function(obj, first) {
                //得到了当前页，用于向服务端请求对应数据
                var curr = obj.curr;
                if(!first) {
                	$("#skipToPage").val(curr);
                	$("#htform").submit();
                }
            }
        });
		
		
		
    });
</script>
</body>
</html>